package com.mach.platform.sprider.dev;



import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;

import java.io.IOException;

/*
    LinkKeWork S7244
 */
public class TemperatureLoraDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public TemperatureLoraDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;

        this.wk.setSoTimeOutValue(4000);
    }

    public static void main(String[] args) throws IOException {
        findAll(args);
    }
    //扫描参数
    public static void findAll(String[] args) throws IOException {
        int busNumStartIx = 6,busNumEndIx = 6; //总线地址开始与结束参数
        int tcpPortStartIx = 502,tcpPortEndIx = 502; //TCP端口开始与结束参数
        String ip = "192.168.6.11";
        for(int mm = 1; mm <= 100000; ++mm){
            for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=1){
                for(int i = busNumStartIx; i <= busNumEndIx; ++i){
                    for(int k = 2; k <= 3; ++k){
                        readIdById(ip,j,i,"XXXX",k );

                    }
                    System.out.println("----------------------------------");
                }
            }
        }

    }
    public static void setId(Integer id) throws IOException {
        TemperatureLoraDev dev =  new TemperatureLoraDev("192.168.1.200",30028,2,"XXXX" );
        byte[] data = dev.readSth(10, 1, 3);
        UU.printData(data);
        byte[] data2 = dev.setDataRtu(10,5);
        UU.printData(data2);
    }
    public static void readIdById(String ip ,Integer port, Integer id,String devName, Integer addr) throws IOException {
        TemperatureLoraDev dev =  new TemperatureLoraDev(ip,port,id,devName);
//        byte[] data = dev.readSth(addr, 1);
//        UU.printData(data);
        Short temp = dev.getTemp(addr,2);
        System.out.println("temp=" + temp + " addr=" + addr);
    }



    public byte[] readSth(Integer addr, Integer num, Integer cmdCode){
        byte[] result = null;


        byte[] repData = new byte[0];
        try {
            repData = this .getDataRtu(addr,num);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = repData;
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }
        return result;
    }

    public Short getTemp(Integer addr, Integer num){
        Short result = null;


        byte[] repData = new byte[0];
        try {
            repData = this .getDataRtu(addr,num);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,2);
            data = ByteUtil.reverse(data);
            short data2Int = ByteUtil.getShort(data);
            result = new Short(data2Int);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }
        return result;
    }



    public byte[] getDataRtu(Integer startAddr, Integer len) throws IOException {
        IntNum startAddrNum = new IntNum(startAddr);
        IntNum lenNUmum = new IntNum(len);

        byte[] result = {};

        byte[] sendData = new byte[6];

        sendData[0] = this.devID.byteValue();
        sendData[1] = 0x21;
        sendData[2] = startAddrNum.h2();
        sendData[3] = startAddrNum.l2();
        sendData[4] = lenNUmum.h2();
        sendData[5] = lenNUmum.l2();

        byte[] sendDataHasCRC = UU.setCrc16Btye(sendData);

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendDataHasCRC);
//        		this.wk.send(sendData);
//		result = this.wk.read4TimeOut();

        result = this.wk.command(sendDataHasCRC);

        System.out.print("devId:" + this.getDevAddr() + " --> ");//debug log
        UU.printData(result);


        return result;
    }

    public byte[] getDataTcp(Integer startAddr, Integer len) throws IOException {

        IntNum startAddrNum = new IntNum(startAddr);
        IntNum lenNUmum = new IntNum(len);

        byte[] result = {};

        byte[] sendData = new byte[12];

        sendData[0] = cnt.l2();
        sendData[1] = cnt.h2();
        sendData[2] = 0;
        sendData[3] = 0;
        sendData[4] = 0;
        sendData[5] = 6;

        sendData[6] = this.devID.byteValue();
        sendData[7] = 0x21;
        sendData[8] = startAddrNum.h2();
        sendData[9] = startAddrNum.l2();
        sendData[10] = lenNUmum.h2();
        sendData[11] = lenNUmum.l2();

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendData);

//		this.wk.send(sendData);
//		result = this.wk.read4TimeOut();

        result = this.wk.command(sendData);

        System.out.print("devId:" + this.getDevAddr() + " --> bytes>");//debug log
        UU.printData(result);


        return result;
    }
}
